package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/*套餐业务实现*/
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /*新增套餐*/
    @Transactional
    public void insertSetmealWithDish(SetmealVO setmealVO) {
        //先将套餐信息插入setmeal表中
        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealVO,setmeal);
        setmealMapper.insert(setmeal);

        //插入套餐与菜品关联信息插入
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();

        //补充setmealId这个属性，因为当前还没有套餐生成
        Long setmealId = setmeal.getId();

        for(SetmealDish setmealDish : setmealDishes){
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishMapper.insertBatch(setmealDishes);


    }


    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = new PageResult();

        //分页查询，查category和setmeal的连接表
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> pages = setmealMapper.page(setmealPageQueryDTO);

        pageResult.setTotal(pages.getTotal());
        pageResult.setRecords(pages.getResult());

        return pageResult;
    }

    /**
     * 批量删除套餐
     */
    public void deleteBatch(List<Long> ids) {
        //如果某个套餐id对应的状态是在售，则不能删除
        ids.forEach(id->{
            Setmeal setmeal = setmealMapper.getById(id);

            if(StatusConstant.ENABLE == setmeal.getStatus()){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        //否则要删除setmeal表的数据和，setmeal_dish表对应的数据
        ids.forEach(setmealId -> {
            //SETMEAL
            setmealMapper.deleteById(setmealId);

            //SETMEAL_DISH
            setmealDishMapper.deleteBySetmealId(setmealId);

        });

    }

    /**
     * 根据套餐id查询SetmealVO
     * @param id
     * @return
     */
    public SetmealVO getById(Long id) {
        //需要查两张表setmeal和setmeal_dish
        Setmeal setmeal = setmealMapper.getById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        SetmealVO setmealVO = new SetmealVO();

        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }


    /**
     * 修改套餐  修改setmeal，删除原本该setmeaId对应的菜品，再重新插入
     * @param setmealDTO
     */
    public void update(SetmealDTO setmealDTO) {
        //修改setmeal表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.update(setmeal);

        //删除setmeal_dish中setemal_id之前对应的菜品
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());

        //重新插入，但是要记得还要更新setmeal_id否则是空的
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }

        //重新插入
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 修改售卖状态
     * @param status
     * @param setmealId
     */
    public void TurnON(Integer status, Long setmealId) {
        //修改setmeal表
        // 起售套餐时，如果套餐内有停售的菜品，则不能起售并提示
        if(status == StatusConstant.ENABLE){
            //查询setmeal_dish中对应菜品
            List<Dish> dishes = dishMapper.getBySetmealId(setmealId);

            if(dishes != null && dishes.size() > 0){
                dishes.forEach(dish ->{
                    if(dish.getStatus() == StatusConstant.DISABLE){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        //处理无异常后，开始update
        Setmeal setmeal = Setmeal.builder()
                .id(setmealId)
                .status(status)
                .build();
        setmealMapper.update(setmeal);

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
